/**
 * 小区管理初始化
 */
var Village = {
    id: "VillageTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Village.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle',width:'50px'},
        {title: '小区名称', field: 'vil_name', align: 'center', valign: 'middle', sortable: true},
        {title: '所属地区', field: 'area_name', align: 'center', valign: 'middle', sortable: true}];
};

/**
 * 检查是否选中
 */
Village.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Village.seItem = selected[0];
        return true;
    }
};
/**
 * 点击添加小区
 */
Village.openAddVillage = function () {
    var index = layer.open({
        type: 2,
        title: '添加小区',
        area: ['500px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/village/village_add'
    });
    this.layerIndex = index;
};

/**
 * 编辑小区
 */
Village.openChangeVillage = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改小区',
            area: ['500px', '300px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/village/village_update/' + Village.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除小区
 */
Village.delVillage = function () {
    if (this.check()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/village/delete", function () {
                Feng.success("删除成功!");
                Village.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id",Village.seItem.id);
            ajax.start();
        };

        Feng.confirm("是否刪除该小区?", operation);
    }
};

/**
 * 查询小区列表
 */
Village.search = function () {
    var queryData = {};
    queryData['vil_name'] = $("#vil_name").val();
    Village.table.refresh({query: queryData});
};

Village.resetSearch = function () {
    $("#vil_name").val("");

    Village.search();
}

$(function () {
     var defaultColunms = Village.initColumn();
        var table = new BSTable("VillageTable", "/village/list", defaultColunms);
        table.setPaginationType("client");
        Village.table = table.init();
});
